package com.biom3d.dashboard;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import com.biom3d.R;

import com.biom3d.quickdialog.ActionItem;
import com.biom3d.quickdialog.QuickAction;

public class AndroidDashboardDesignActivity extends Activity {

	// action id
	private static final int ID_FAQ = 1;
	private static final int ID_CONTACT = 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dashboard_layout);

		DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        if(dm.widthPixels < 600) {
        	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

		// Action Items
		ActionItem doubts = new ActionItem(ID_FAQ, "FAQ", getResources()
				.getDrawable(R.drawable.icon_guide));
		ActionItem contact = new ActionItem(ID_CONTACT, "CONTACT",
				getResources().getDrawable(R.drawable.icon_mail));

		// use setSticky(true) to disable QuickAction dialog being dismissed
		// after an item is clicked
		doubts.setSticky(true);
		contact.setSticky(true);

		// create QuickAction. Use QuickAction.VERTICAL or
		// QuickAction.HORIZONTAL param to define layout
		// orientation
		final QuickAction quickAction = new QuickAction(this,
				QuickAction.VERTICAL);

		// add action items into QuickAction
		quickAction.addActionItem(doubts);
		quickAction.addActionItem(contact);

		/**
		 * Creating all buttons instances
		 * */
		// Dashboard Open Image button
		Button btn_open_image = (Button) findViewById(R.id.btn_open);

		// Dashboard Import Image button
		Button btn_import_image = (Button) findViewById(R.id.btn_import);

		// Dashboard About button
		Button btn_about = (Button) findViewById(R.id.btn_about);

		// Dashboard Help button
		Button btn_help = (Button) findViewById(R.id.btn_help);

		// Dashboard Settings button
		Button btn_settings = (Button) findViewById(R.id.btn_settings);

		// Dashboard Exit button
		Button btn_exit = (Button) findViewById(R.id.btn_exit);

		/**
		 * Handling all button click events
		 * */

		// Listening Open Image button click
		btn_open_image.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching Open Image Screen
				Intent i = new Intent(getApplicationContext(),
						OpenImageActivity.class);
				startActivity(i);
			}
		});

		// Listening Import Image button click
		btn_import_image.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching Import Image Screen
				Intent i = new Intent(getApplicationContext(),
						ImportFileActivity.class);
				startActivity(i);
			}
		});

		// Listening About button click
		btn_about.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching About Screen
				Intent i = new Intent(getApplicationContext(),
						AboutActivity.class);
				startActivity(i);
			}
		});

		// Listening Help button click
		btn_help.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				quickAction.show(view);
				quickAction.setAnimStyle(QuickAction.ANIM_REFLECT);
				// Launching Help Screen
				// Intent i = new Intent(getApplicationContext(),
				// HelpActivity.class);
				// startActivity(i);
			}
		});

		// Listening to Settings button click
		btn_settings.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Launching Settings Screen
				Intent i = new Intent(getApplicationContext(),
						SettingsActivity.class);
				startActivity(i);
			}
		});

		// Listening to Exit button click
		btn_exit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// Setting Attributes
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(
						AndroidDashboardDesignActivity.this);
				alertDialog.setTitle("Are you sure?");
				// alertDialog.setMessage("Are you sure?");
				alertDialog.setIcon(R.drawable.btn_exit);

				// Setting Positive "Yes" Button
				alertDialog.setPositiveButton("YES",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}
						});

				// Setting Negative "NO" Button
				alertDialog.setNegativeButton("NO",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								// Write your code here to invoke NO event
								// Toast.makeText(getApplicationContext(),
								// "Action cancelled", Toast.LENGTH_SHORT)
								// .show();
								dialog.cancel();
							}
						});

				// Showing Alert Message
				alertDialog.show();
			}
		});

		// ----- QUICK ACTIONS -----

		// Set listener for action item clicked
		quickAction
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					@Override
					public void onItemClick(QuickAction source, int pos,
							int actionId) {
						// ActionItem actionItem =
						// quickAction.getActionItem(pos);

						// here we can filter which action item was clicked with
						// pos or actionId parameter
						if (actionId == ID_FAQ) {
							Intent i = new Intent(getApplicationContext(),
									FAQActivity.class);
							startActivity(i);
						} else if (actionId == ID_CONTACT) {
							Intent i = new Intent(getApplicationContext(),
									ContactActivity.class);
							startActivity(i);
						}
					}
				});

		// set listnener for on dismiss event, this listener will be called only
		// if QuickAction dialog was dismissed
		// by clicking the area outside the dialog.
		// quickAction.setOnDismissListener(new QuickAction.OnDismissListener()
		// {
		// @Override
		// public void onDismiss() {
		// Toast.makeText(getApplicationContext(), "Dismissed",
		// Toast.LENGTH_SHORT).show();
		// }
		// });
	}
}